package Frame.battleanimation;

import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import VWorldGameStudio.FCAnimation;
import VWorldGameStudio.FCItem;

import java.awt.*;

import static FCRendering.FCRMaster.fcv;
import static FCRendering.FCRMaster.itemLib;

//投掷道具的
public class ThrowItemAni extends ABattleAni implements IBattleAni {

    //原点
    protected int[] startPoint;

    //结束点
    protected int[] endPoints;

    protected int flyFrame = 0;

    protected int[] flyPaintStart = new int[2];

    protected double targetDistance;
    protected String name;
    private String itemName;
    FCItem fcItem;
    private int endFrame = 25;
    private boolean throwToenemy = true;//默认是对敌人使用
    private boolean stopShowThrowItem = false;
    private boolean canHit = true;
    /**
     * 没特效的扔道具
     * @param itemName
     * @param throwToenemy
     * @param name
     * @param startPoint
     * @param endpoint
     */
    public ThrowItemAni(String itemName, boolean throwToenemy, String name, int[] startPoint, int[] endpoint, boolean isPlayerAttackMode) {
        this.name = name;
        this.itemName = itemName;
        this.isPlayerAttackMode = isPlayerAttackMode;
        fcItem = itemLib.getItemOfID(itemName);
        this.startPoint = startPoint;
        this.endPoints = endpoint;
        //STEP 1计算俩点之间距离，速度假设是v像素/毫秒，
        int v = (int) (750*fcv.getProportion());
        targetDistance = AnimationUtil.getDistance(startPoint, endpoint);
        flyFrame = (int) (16 * targetDistance / v);

        if (fcItem == null) {
            allFrames = 5 + endFrame;
        } else {
            allFrames = flyFrame + endFrame;
            allFrames = Math.max(5, allFrames);
        }
        if(isPlayerAttackMode) {
            playerOffset[0] = (int) (20 * fcv.getProportion());
            startPoint[0] -= playerOffset[0];
        }else {
            playerOffset[0] = (int) (40 * fcv.getProportion());
            startPoint[0] += playerOffset[0];
        }
        if(endpoint[0] == 0 || endpoint[0] >= (int)(1920*fcv.getProportion())){
            canHit = false;
        }
    }


    public void nextFrame(int frame) {
        //TODO 在这里必须计算好paintPoints 和startPngIndex这些
        if (fcItem != null) {
            if(frame <= flyFrame) {
                AnimationUtil.getAnimatePoint(flyPaintStart, startPoint, endPoints, (float) frame / flyFrame);
            }else {
                stopShowThrowItem = true;
            }
        }
        if (throwToenemy && canHit) {
            if (frame >= flyFrame) {
                //爆炸了，可以将敌人闪俩下
                int cha = frame - flyFrame;
                if (cha < endFrame / 4) {
                    beHitShowVisable = false;
                } else if (cha < endFrame / 2) {
                    beHitShowVisable = true;
                } else if (cha < 3 * endFrame / 4) {
                    beHitShowVisable = false;
                } else {
                    beHitShowVisable = true;
                }
            }
        }

        if (frame == 5) {
//            fcv.getMessage().add(
//                    FCRConstants.SYSTEM + FCRConstants.BATTLE + name + " " + FCRConstants.BE_THROW + itemName);
//            fcv.setMessageShowNum(300);
        }
    }

    @Override
    public void paintBattleSceen(Graphics g, FCRMaster fcrMaster) {
        if (fcItem != null && !stopShowThrowItem) {
            flying(g, fcrMaster);
        }
    }


    @Override
    public void fire(Graphics g, FCRMaster fcrMaster) {
    }

    @Override
    public void flying(Graphics g, FCRMaster fcrMaster) {
        g.drawImage(fcItem.getPngPng(), flyPaintStart[0], flyPaintStart[1],
                (int) (32 * fcv.getProportion()), (int) (32 * fcv.getProportion()), fcrMaster);
    }

    @Override
    public void boom(Graphics g, FCRMaster fcrMaster) {
    }
}
